#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>
#include "http.h"

char* api_key = "bce-v3/...";
char* app_id = "38b39091-0040-4190-aec6-b271aa9233ef";
char* conversation_id;

char* new_conversation(char* app_id, char* api_key)
{
    char *url = "https://qianfan.baidubce.com/v2/app/conversation";
    
    //构造请求消息
    cJSON* root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "app_id", app_id);
    char *postdata = cJSON_Print(root);
    cJSON_Delete(root);
    puts(postdata);
    
    //增加请求头部字段
    struct curl_slist* headers = NULL;
    headers = curl_slist_append(headers, "Content-Type: application/json");
    char* auth = NULL;
    asprintf(&auth, "Authorization: Bearer %s", api_key);
    headers = curl_slist_append(headers, auth);
    free(auth);
    
    size_t size = strlen(postdata);
    char* response = post(url, headers, postdata, &size);
    curl_slist_free_all(headers);
    free(postdata);

    if (!response)
    {
        return NULL;
    }

    puts(response);

    cJSON *resp_root = cJSON_ParseWithLength(response, size);
    free(response);

    if (!resp_root)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        return NULL;
    }

    cJSON* conversation_id = cJSON_GetObjectItem(resp_root, "conversation_id");
    if (!conversation_id)
    {
        cJSON* msg = cJSON_GetObjectItem(resp_root, "message");
        if (msg)
        {
            fprintf(stderr, "Error: %s\n", msg->valuestring);
        }
        cJSON_Delete(resp_root);
        return NULL;
    }

    char* retval = strdup(conversation_id->valuestring);

    cJSON_Delete(resp_root);
    return retval;
}

char* conversation(char* app_id, char* api_key, char* conversation_id, char* query)
{
    char *url = "https://qianfan.baidubce.com/v2/app/conversation/runs";

    //构造请求消息
    cJSON* root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "app_id", app_id);
    cJSON_AddStringToObject(root, "conversation_id", conversation_id);
    cJSON_AddFalseToObject(root, "stream");
    cJSON_AddStringToObject(root, "query", query);
    char *postdata = cJSON_Print(root);
    cJSON_Delete(root);
    puts(postdata);

    //增加请求头部字段
    struct curl_slist* headers = NULL;
    headers = curl_slist_append(headers, "Content-Type: application/json");
    char* auth = NULL;
    asprintf(&auth, "Authorization: Bearer %s", api_key);
    headers = curl_slist_append(headers, auth);
    free(auth);

    size_t size = strlen(postdata);
    char* response = post(url, headers, postdata, &size);
    curl_slist_free_all(headers);
    free(postdata);

    if (!response)
    {
        return NULL;
    }

    puts(response);

    cJSON *resp_root = cJSON_ParseWithLength(response, size);
    if (!resp_root)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        return NULL;
    }

    cJSON* answer = cJSON_GetObjectItem(resp_root, "answer");
    if (!answer)
    {
        cJSON* msg = cJSON_GetObjectItem(resp_root, "message");
        if (msg)
        {
            fprintf(stderr, "Error: %s\n", msg->valuestring);
        }
        cJSON_Delete(root);
        return NULL;
    }

    char* retval = strdup(answer->valuestring);

    cJSON_Delete(resp_root);
    return retval;
}

int main()
{
    char line[80];

    conversation_id = new_conversation(app_id, api_key);
    if (!conversation_id)
    {
        return 1;
    }

    while (fgets(line, sizeof line, stdin))
    {
        char* answer = conversation(app_id, api_key, conversation_id, line);
        if (answer)
        {
            puts(answer);
            free(answer);
        }
    }

    free(conversation_id);

    return 0;
}